package com.game.service.socket;

import com.game.service.beans.RoomInfo;
import com.game.service.beans.UserInfo;
import com.game.service.robot.No1Robot;
import com.game.service.robot.No2Robot;

import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

public class GameControl {
    //必须要玩家大于两个人(正常设置应该为玩家数量在10人,两边之差大于1000,)

    //1.顶分机器人,顶分比例为10%到75%(默认设置)激活条件，房间内玩家大于3人。两边之差大于1000.
    //2.陪玩机器人，押注为系统的A:10%~30%(P:80%),A:30%~50%(P:10%),A:50%~70%(B:5%),70%~100%(P:5%)。先随机P陪玩概率，在随机押注分数(A概率范围x(总分之差))，陪玩机器人会输分。
    //3.抽杀机器人，即强行改变游戏结果,激活条件，如果开启，检测到某一边押注过大，则安排机器人进去。然后开相反的，抽杀几率为5%.(限制条件，必须是dragon=1,才能抽杀)
    //          限制条件，通常出现长龙的时候比较容易有玩家去斩龙。(最后再开发)
    //

    //平衡模式(默认),庄家模式(无)，双押模式（未开发）,退分模式(先后退分，比例退分)。清分(不能清,或者只能清除最近一次的)
    No1Robot no1Robot;
    No2Robot no2Robot;
    private SocketServer SS;

    public GameControl() {
        no1Robot = createNo1Robot();
        no2Robot = createNo2Robot();
    }

    private No1Robot createNo1Robot() {
        No1Robot no1Robot = new No1Robot();

        UserInfo userInfo = new UserInfo();
        userInfo.id = 100;
        userInfo.score = 1000000;
        userInfo.name = "1号机器人";
        userInfo.status = 1;
        //
        no1Robot.userInfo = userInfo;
        no1Robot.factor = new int[]{1000, 2, 20};
        no1Robot.isOpen = true;
        no1Robot.isAdd = false;
        no1Robot.minRatio = 10;
        no1Robot.maxRatio = 75;

        return no1Robot;
    }


    private No2Robot createNo2Robot() {
        No2Robot no2Robot = new No2Robot();

        UserInfo userInfo = new UserInfo();
        userInfo.id = 200;
        userInfo.score = 1000000;
        userInfo.name = "2号机器人";
        userInfo.status = 1;
        no2Robot.userInfo = userInfo;
        no2Robot.isOpen = true;
        no2Robot.isAdd = false;

        no2Robot.step1 = 50;
        no2Robot.minRatioStep1 = 10;
        no2Robot.maxRatioStep1 = 30;

        no2Robot.step2 = 30;
        no2Robot.minRatioStep2 = 30;
        no2Robot.maxRatioStep2 = 50;

        no2Robot.step3 = 10;
        no2Robot.minRatioStep3 = 50;
        no2Robot.maxRatioStep3 = 75;

        no2Robot.step4 = 10;
        no2Robot.minRatioStep4 = 75;
        no2Robot.maxRatioStep4 = 100;


        return no2Robot;
    }


    public No1Robot getNo1Robot() {
        return no1Robot;
    }

    public No2Robot getNo2Robot() {
        return no2Robot;
    }


    public int random(int min, int max) {

        return ThreadLocalRandom.current().nextInt(min, max + 1);

    }


    public void init(SocketServer socketServer) {
        SS = socketServer;
    }


    public void deal() {
//        if (no1Robot.isAdd) {
//            //已经顶分了
//            return;
//        }

        RoomInfo roomInfo = SS.roomInfo;
        int aa = random(no1Robot.minRatio, no1Robot.maxRatio);
        long bb = roomInfo.bigTotal - roomInfo.smallTotal;
        UserInfo userInfo = no1Robot.userInfo;
        userInfo.status = 1;
        final long cc = (aa * Math.abs(bb)) / 1000 * 10;
        userInfo.betScore = cc;
        boolean openBig = SS.getGamePoints() > 10;

        if (!openBig && bb >= 0) {
            userInfo.select = 2;
        }
        if (openBig && bb < 0) {
            userInfo.select = 1;
        }

        boolean factor1 = Math.abs(bb) > no1Robot.factor[0];
        boolean factor2 = 2 == no1Robot.factor[1];
        boolean factor3 = random(1, 100) < no1Robot.factor[2];

        if (factor1 & factor2 & factor3) {
            SS.GameBetReturn(null, userInfo);
            System.out.println("1号机器人押注"+userInfo);
            no1Robot.isAdd = true;
        }


    }

    public void playerRobot() {
        if (no2Robot.isAdd) {
            return;
        }

        RoomInfo roomInfo = SS.roomInfo;
        final int step = random(1, 100);
        int aa = 0;
        final int step1 = no2Robot.step1;
        if (step <= step1) {
            aa = random(no2Robot.minRatioStep1, no2Robot.maxRatioStep1);
        }
        final int step2 = no2Robot.step1 + no2Robot.step2;
        if (step > step1 && step <= step2) {
            aa = random(no2Robot.minRatioStep2, no2Robot.maxRatioStep2);
        }
        final int step3 = no2Robot.step1 + no2Robot.step2 + no2Robot.step3;
        if (step > step2 && step <= step3) {
            aa = random(no2Robot.minRatioStep3, no2Robot.maxRatioStep4);
        }
        final int step4 = no2Robot.step1 + no2Robot.step2 + no2Robot.step3 + no2Robot.step4;
        if (step > step3 && step <= step4) {
            aa = random(no2Robot.minRatioStep4, no2Robot.maxRatioStep4);
        }

        long bb = roomInfo.bigTotal - roomInfo.smallTotal;
        UserInfo userInfo = no2Robot.userInfo;
        userInfo.status = 1;
        final long cc = (aa * Math.abs(bb)) / 1000 * 10;//最小押注为10的处理方法;
        userInfo.select = random(1, 2);
        userInfo.betScore = cc;
        if (cc >= 10) {
            SS.GameBetReturn(null, userInfo);
            no2Robot.isAdd = true;
        }

    }
}
